package repository.relsys.services;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import repository.relsys.domainmodel.general.*;

import repository.relsys.services.datacentral.FileDataProvider;
import repository.relsys.services.datacentral.RepositoryDataProvider;
import repository.relsys.services.spring.RelsysResourceFactory;

public class RelsysReadWrite extends DefaultRelsysService {

	public RelsysReadWrite() {

		dataProvider = new RepositoryDataProvider();
		initialize();

	}

	@SuppressWarnings("unchecked")
	public <T extends Resource> T loadResourceFromDB(Class<T> clazz) throws Exception {

		RelsysResourceFactory rf = new RelsysResourceFactory();

		Collection<T> c = (Collection<T>) loadAllResources(rf
				.getBeanImplementationClass(clazz), true);

		Iterator<T> iter = c.iterator();

		return iter.next();

	}
	
	
	@SuppressWarnings("unchecked")
	public <T extends Resource> Collection<T> loadResourcesFromDB(Class<T> clazz) throws Exception {

		RelsysResourceFactory rf = new RelsysResourceFactory();

		Collection<T> c = (Collection<T>) loadAllResources(rf
				.getBeanImplementationClass(clazz), true);

		

		return c;

	}

	
	public <T extends Resource> void saveResources(Collection<T> resources) throws Exception {

		LinkedList<T> list = (LinkedList<T>) resources;

		Iterator<T> iter = list.iterator();

		while (iter.hasNext()) {
			T t = (T) iter.next();

			
			saveResource(t, true);
				
				}
		
		
	}

	
	public <T extends Resource> void saveResource(T resource) throws Exception {

		saveResource(resource, true);
	}



}
